Wiki Guide to PHP:Tutorial - Basics of php
PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a server-side scripting language designed for use in web based applications. As it is a server side scripting language, its use requires a web server installed, configured and enabled with PHP. Prerequisites There are no prerequisites for this course. However, it is strongly reccommended that you have access to a php enabled webserver or install WAMP/LAMP on your system to practise. Basic Syntax When PHP parses a file (web servers are generally configured to have php parse files with the .php extension), it looks for an opening tag and a closing tag and then interprets the code between the two: This is not being parsed. PHP will output everything that is not contained within an opening tag () and a closing tag (?> or ) exactly as it is written. All lines of code in PHP should end with a semicolon. This denotes to the PHP interpreter that a command has been passed and that it should anticipate the next (or the ?> tag / end of file). This is because different text editors handle new lines differently (the Notepad utility on Microsoft Windows is notorious for being terrible with UNIX end of line compatibility), and it allows coders to squeeze together small blocks of code on the same line. For example, the following is acceptable: And will work just the same as the example below. Variables PHP scripts can contain variables, which are denoted with the $ at the start of their unique identifier (two variables with the same identifier are, essentially, the same variable). Unlike many low end languages, variables in PHP need not be declares and defined - when a variable is used, PHP automatically creates it and assigns it a null value if it does not already exist. Take a look at this example: Notice the difference from the first example - in this, we assign the sentence This is being parsed. to the variable $message and then output it. This may seem like a pointless example, but rest assured, variables have a very important purpose in PHP. Comments PHP supports commenting code for ease of viewing by humans. There are two common ways of commenting ways: comment blocks and line commenting. Comment blocks Comment blocks work by denoting a range from one point to another within a script as a comment, which is then ignored entirely by the PHP interpreter. They can be inserted anywhere (although it is good practise to give them their own few lines in the script for readability's sake) and are denoted by two special characters: / and *. Take a look at this example: Notice that the order of the / and * alternate. To designate an area in a script as a comment block, simply begin it with a /* and end with a */. Comments can, as mentioned above, be placed anywhere like so: Notice that comment blocks are placed between the echo function and its parameters, within the function call for echo itself (ec/* ... */ho), within the string passed to the echo function and just before the end of line declaration (with the semicolon). Line commenting Line commenting is much like the comment block, except that it only affects a particular line from a given point onwards, cannot be ended until the next line and only uses the // operator. Examine the following example: Anything after a // on a line is considered a comment, ignored entirely by the interpreter and meaningless to anything but a human examining the code. Remember that the use of echo 'This will NOT be parsed.'; above is totally immaterial; the word echo will not be treated as a function call. A // can also be added to the end of a line (must be after the semicolon if there is a function call on the same line) and anything after it will be commented, like so: Functions and arguments PHP is well known for its extensive library of inbuilt functions. A function in php is a set of code predefined to be called by a particular keyword, either built into PHP's numerous libraries, or defined by the person writing the script within the code. A function can be declared like so: In the same script, the function can then be called like this: This will output 'Stuff.' (without the quotation marks). Functions can have arguments which can be passed when the function is called: In this example, the variable $extrastuff does not need to be sent to the function as an argument, however if it is, it will be output after 'Stuff.'. Multiple arguments are seperated by a comma, however do not need seperate sets of parenthesis (in fact, this will give you a compile error).